package com.lucaswangdev.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

@Configuration
public class OssConfig {
    /**
     * 将 <YOUR-ENDPOINT> 替换为 Endpoint，例如 oss-cn-hangzhou.aliyuncs.com
     */
    private String endpoint = "";

    public String getBucket() {
        return bucket;
    }

    /**
     * 将 <YOUR-BUCKET> 替换为 Bucket 名称
     */
    private String bucket = "oss-testxxx";
    /**
     * 指定上传到 OSS 的文件前缀
     */
    private String dir = "img/";
    /**
     * 指定过期时间，单位为秒
     */
    private long expireTime = 3600;
    /**
     * 构造 host
     */
    private String host = "http://" + bucket + "." + endpoint;
    /**
     * 通过环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 设置 accessKeyId
     */
    // private String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    private String accessKeyId ="";
    /**
     * 通过环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET 设置 accessKeySecret
     */
    // private String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    private String accessKeySecret = "";

    private OSS ossClient;
    @Bean
    public OSS getOssClient() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }
    @Bean
    public String getHost() {
        return host;
    }
    @Bean
    public String getAccessKeyId() {
        return accessKeyId;
    }
    @Bean
    public long getExpireTime() {
        return expireTime;
    }
    @Bean
    public String getDir() {
        return dir;
    }

    @PreDestroy
    public void onDestroy() {
        ossClient.shutdown();
    }
}
